// pages/addAppoPeople/addAppoPeople.js
const APP = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    peopleList: [], // 获取添加预约人的预约人列表
    userType: [], //用户类型字典，
    relationType: [], //用户关系字典
    peopleOptions: [], //传到后台的选中数组  
    deleteArr: '', //删除的多选字符串  ，   隔开 
  },
  //confirmChoose  点击确定选择
  confirmChoose() {
    // this.setData
    var peopleOptions = this.data.peopleOptions
    // console.log('选中的数组', peopleOptions)
    APP.setStorage('peopleOptions', peopleOptions)
    wx.navigateBack()
  },
  //点击顶部删除
  clickDelete() {
    let that = this
    var peopleOptions = this.data.peopleOptions
    var length = peopleOptions.length

    if (length < 1) {
      wx.showToast({
        title: '未选择任何数据',
        icon: 'none',
        duration: 1000
      })
    } else {
      console.log(this.data.deleteArr);

      wx.showModal({
        title: '提示',
        content: '确定删除吗',
        success(res) {
          if (res.confirm) {
            var postObj = {
              url: '/mobileUser/interviewer/delete/' + that.data.deleteArr,
              method: 'delete',
            }
            APP.request.wxRequest(postObj).then(res => {
              if (res.data.code === 200) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1000
                })
                that.onShow()
              }
            })

          } else if (res.cancel) {
            // wx.showToast({
            //   title: '删除取消',
            //   icon: 'none',
            //   duration: 1000
            // })
          }
        }
      })
    }

  },
  //点击复选框
  onChangeCheck(e) {
    var index = e.currentTarget.dataset.index
    var data = `peopleList[${index}].checked`
    this.setData({
      [data]: !this.data.peopleList[index].checked
    })
    this.choosePeopleOptions()
  },
  //每次点击时候筛选出选中的数据
  choosePeopleOptions() {
    var peopleList = this.data.peopleList
    var peopleOptions = []
    var arr = '' //给后端传字符串删除
    this.setData({
      deleteArr: ''
    })
    peopleList.forEach((items, i) => {
      if (items.checked == true) {
        peopleOptions.push(items)
        if (i == 0) {
          arr = arr + items.interviewerId
        } else {
          arr = arr + ',' + items.interviewerId
        }
      }

    })
    this.setData({
      peopleOptions: peopleOptions,
      deleteArr: arr,
    })
  },
  //跳转到新增预约人
  toAddOneNavigate() {
    wx.navigateTo({
      url: '/pages/addOne/addOne',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  personInterviewerList() {
    var postObj = {
      url: '/mobileUser/interviewer/list',
      method: 'get',
    }

    APP.request.wxRequest(postObj).then(res => {
      if (res.data.data.length > 0) {
        // var peopleOptions = 
        var peopleList = res.data.data
        peopleList.forEach((items, i) => {
          var typeName = APP.selectDictLabel(this.data.relationType, items.relation)
          var userType = APP.selectDictLabel(this.data.userType, items.type)
          items.typeName = typeName
          items.userType = userType
          items.checked = false
        })
        this.setData({
          peopleList: peopleList
        })
        console.log('预约人列表', this.data.peopleList);

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    // 一进入添加预约人就把本地的预约人删除  让用户再手动选择
    wx.removeStorage({
      key: 'peopleOptions',
    })
    APP.getFrontInfo().then(res => {})
    // 律师 家属   类型字典
    APP.getDict('sys_mobile_user_type').then(res => {
      console.log('用户类型字典', res)
      this.setData({
        userType: res.data.data
      })
      // 预约人列表
      // that.personInterviewerList()
    })
    // 用户关系字典   父子等
    APP.getDict('sys_relation').then(res => {
      console.log('用户关系字典', res)
      this.setData({
        relationType: res.data.data
      })
      // 预约人列表
      that.personInterviewerList()
    })



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})